/**
 * 
 */
package com.slayer.service.persistence;

import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.SQLQuery;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
import com.liferay.util.dao.orm.CustomSQLUtil;
import com.slayer.model.BMSBook;
import com.slayer.model.impl.BMSBookImpl;

import java.util.List;

/**
 * @author Hemant
 *
 */
public class BMSBookFinderImpl 
	extends BasePersistenceImpl<BMSBook> 
		implements BMSBookFinder {
		
		static String FIND_BOOKS = BMSBookFinderImpl.class.getName() + ".findBooks";
		
		public List<BMSBook> findBooks(String bookTitle) 
				throws SystemException{
			
			// 1.open a orm session
			Session session = openSession();
			
			// 2.Get sql statement from xml file with its name
			String sql = CustomSQLUtil.get(FIND_BOOKS);
			
			// 3. Transform the normal query to HQL query
			SQLQuery query = session.createSQLQuery(sql);
			
			// 4. add the actual entity to be searched
			query.addEntity("BMSBook", BMSBookImpl.class);
			
			// 5. replace positional parameters in the query
			QueryPos qPos = QueryPos.getInstance(query);
			qPos.add(bookTitle);
			
			// 6. exceute query and return results
			return (List<BMSBook>) query.list();
		}
}
